package com.wealth.scsj.p_app.view.activity.wallet;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.orhanobut.logger.Logger;
import com.wealth.scsj.p_app.R;
import com.wealth.scsj.p_app.adapter.BankCardListAdapter;
import com.wealth.scsj.p_app.base.CashBalanceBase;
import com.wealth.scsj.p_app.bean.BaseBean;
import com.wealth.scsj.p_app.bean.GetOwnTreasureInfoBean;
import com.wealth.scsj.p_app.bean.SelectUserHasBindBankCardBean;
import com.wealth.scsj.p_app.presenter.CashBalancePresenter;
import com.wealth.scsj.p_app.view.activity.BaseActivity;

import java.util.List;

import okhttp3.RequestBody;

/**
 * 余额提现
 */
public class CashBalanceActivity extends BaseActivity implements View.OnClickListener, CashBalanceBase.IView {

    /**
     * 中国建设银行储蓄卡（1624）
     */
    private TextView mBankCardDetails;
    /**  */
    private EditText mMoneyTv;
    /**
     * 当前可提现金额:0.00元，
     */
    private TextView mWithdrawalAmountTv;
    /**
     * 全部提现
     */
    private TextView mAllWithdrawalTv;
    /**
     * 确认提现
     */
    private Button mWithdrawalBtn;
    /**
     * 本次提现将收取您15%手续费，审核通过之后将打款到您提现账户，请耐心等待，超过12小时未收到打款，请联系客服。
     */
    private TextView mCashBalanceState;


    private RecyclerView mCashBalanceRecyclerview;
    private RelativeLayout mCloseRl;
    private TextView mRightTitle;
    private CashBalancePresenter presenter;
    private List<SelectUserHasBindBankCardBean.DataBean.ListBean> bankCardList;
    private View mBankCardPopView;
    private PopupWindow mPopupWindow;
    private double balance;
    private double rate;


    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_cash_balance);
//        ScreenAdapterTools.getInstance().loadView((ViewGroup) getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句

        presenter = new CashBalancePresenter(this);

        initToolbar();
        initView();
        initData();
        setToolbarTitle("余额提现");
        hasBack(true);

        JSONObject object = new JSONObject();
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), object.toString());
        presenter.deliverQuerybankCardData(body);//获取银行卡列表
        presenter.deliverGetOwnTreasureInfoData(body);//我要提现


    }

    @Override
    protected void initView() {

        mBankCardDetails = (TextView) findViewById(R.id.bank_card_details);
        mBankCardDetails.setOnClickListener(this);
        mMoneyTv = (EditText) findViewById(R.id.money_tv);
        mWithdrawalAmountTv = (TextView) findViewById(R.id.withdrawal_amount_tv);
        mAllWithdrawalTv = (TextView) findViewById(R.id.all_withdrawal_tv);
        mAllWithdrawalTv.setOnClickListener(this);
        mWithdrawalBtn = (Button) findViewById(R.id.withdrawal_btn);
        mWithdrawalBtn.setOnClickListener(this);
        mCashBalanceState = (TextView) findViewById(R.id.cash_balance_state);
        mRightTitle = findViewById(R.id.toolbar_title_right);

        mBankCardPopView = View.inflate(this, R.layout.item_pop_cash_balance, null);

        //银行卡弹窗的Popwindow
        mCashBalanceRecyclerview = (RecyclerView) mBankCardPopView.findViewById(R.id.cash_balance_recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mCashBalanceRecyclerview.setLayoutManager(linearLayoutManager);
        mCashBalanceRecyclerview.addItemDecoration(new DividerItemDecoration(this, 1));

        mCloseRl = (RelativeLayout) mBankCardPopView.findViewById(R.id.close_rl);


    }

    @Override
    protected void initListener() {
        mRightTitle.setOnClickListener(this);
        mCloseRl.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        mRightTitle.setText("提现记录");
        mRightTitle.setVisibility(View.VISIBLE);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bank_card_details:
                showpop();
                break;
            case R.id.all_withdrawal_tv:
                break;
            case R.id.withdrawal_btn:

                break;
            case R.id.close_rl:
                if (mPopupWindow != null && mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
                break;
            case R.id.toolbar_title_right:
//                SkipUtils.toWithdrawalRecord(this);
                break;
        }
    }

    private void showpop() {

        if (mPopupWindow != null && mPopupWindow.isShowing()) return;
        mPopupWindow = new PopupWindow(mBankCardPopView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //设置背景半透明
        backgroundAlpha(0.5f);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setOnDismissListener(new CashBalanceActivity.popupDismissListener());
        mPopupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);

    }


    @Override
    public void vQuerybankCardSuccessMsg(String s, SelectUserHasBindBankCardBean bean) {
        String s1 = new Gson().toJson(bean);
        Logger.e(s1);

        bankCardList = bean.getData().getList();

        BankCardListAdapter bankCardListAdapter = new BankCardListAdapter(bankCardList,0);
        mCashBalanceRecyclerview.setAdapter(bankCardListAdapter);
    }

    @Override
    public void vGetOwnTreasureInfoSuccessMsg(String s, GetOwnTreasureInfoBean bean) {
        String s1 = new Gson().toJson(bean);
        Logger.e(s1);
        balance = bean.getData().getBalance();
        rate = bean.getData().getRate();

    }

    @Override
    public void vWithdrawCashSuccessMsg(String s, BaseBean bean) {
        String s1 = new Gson().toJson(bean);
        Logger.e(s1);

    }

    @Override
    public void showErrorMsg(String s) {

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();

        lp.alpha = bgAlpha; //0.0-1.0
        this.getWindow().setAttributes(lp);
    }

    /**
     * 将背景透明度改回来
     */
    class popupDismissListener implements PopupWindow.OnDismissListener {
        @Override
        public void onDismiss() {
            backgroundAlpha(1f);
        }
    }
}
